package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.ProfitAdapter;
import com.dlwx.firefighting.bean.ProfitBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProfitActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;

    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private ProfitAdapter mAdapter;
    private String userId;
    private int page=1;
    private int requestNum=10;
    private int count=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profit);
        ButterKnife.bind(this);
        userId= (String) SPUtils.get(this, Constant.USER_ID,"");
        initView();
        initRecy();

    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("分红");
    }

    private void getDatas() {
        OkGo.<String>post(Constant.PROFIT)
                .params("user_id",userId)
                .params("num",requestNum)
                .params("page",page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<ProfitBean> beans=dealResult.deal(ProfitActivity.this,response,new TypeToken<BaseBean<ProfitBean>>(){}.getType());
//                        setDatas(beans);
                    }
                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(ProfitActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<ProfitBean> beans) {
       /* if(beans.isEmpty()){
            return;
        }*/
        mAdapter.addAll(beans);
        recyclerView.refreshComplete(requestNum);
    }

    private void initRecy() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ProfitAdapter(ProfitActivity.this);
        mAdapter.setDataList(new ArrayList<ProfitBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.divider_height)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_gray)
                .build();

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);

        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.refresh();

        //设置下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(true);

        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载文字提示
        recyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if(count<(page*requestNum)){
                    ToastUtils.toastForShort(ProfitActivity.this,getString(R.string.no_more));
                    recyclerView.setNoMore(true);
                }else{
                    page++;
                    getDatas();

                }
            }
        });
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();

            }
        });

        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }
        });

    }

    /**
     * 刷新
     */
    private void refresh() {
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        page=1;
        getDatas();
    }


    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
        }
    }
}
